<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" :src="value" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- x图标 -->
    <i v-show="value" class="el-icon-error i_icon" @click="iconClick" />

    <!-- <el-progress
      v-show="progress > 0"
      type="circle"
      :percentage="progress"
      :width="50"
    /> -->
    <!-- 进度条 -->
    <transition name="depa">
      <el-progress
        v-if="progressShow"
        :percentage="progress"
        :stroke-width="3"
      />
    </transition>
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDviHbnocAUaS5TedOvDF1rTs55rPimbAU',
  SecretKey: '82tZ1DAsPhxBHDxUpwpTr1wb092f290b'
})
export default {
  name: 'UploadPhoto',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      progress: 0,
      progressShow: false
    }
  },
  created () {},
  methods: {
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest (data) {
      this.progress = 0
      this.progressShow = true
      console.log('data', data)
      cos.putObject(
        {
          Bucket: '111-1308171925' /* 存储桶 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: data.file.uid + '' /* 标识 */,
          StorageClass: 'STANDARD', // 固定值
          Body: data.file, // 上传文件对象  file文件对象
          onProgress: progressData => {
            // 上传进度，实时返回上传文件进度
            console.log(JSON.stringify(progressData))
            this.progress = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.progressShow = false
              }, 1000)
            }
          }
        },
        (err, data) => {
          // 上传成功或者失败处理
          // err为空就是成功，err有值就是失败了
          // data就是上传成功后腾讯云返回的数据（上传文件地址）
          console.log('1', err, data)
          if (!err) {
            this.$emit('input', 'http://' + data.Location)
          }
        }
      )
    },
    iconClick () {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  background-color: #fff;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  width: 178px;
  position: relative;
  &:hover {
    .i_icon {
      display: block;
    }
  }
  .i_icon {
    display: none;
    position: absolute;
    top: 5px;
    right: 5px;
    cursor: pointer;
    color: #eee;
    font-size: 20px;
  }
  .el-progress {
    text-align: center;
  }
  .depa-enter-active,
  .depa-leave-active {
    transition: all 0.5s;
  }
  .depa-enter,
  .depa-leave-to {
    opacity: 0;
  }
}
</style>
